<?php

use kartik\date\DatePicker;
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use kartik\file\FileInput;
use yii\web\JsExpression;
use backend\assets\PrintAsset;
PrintAsset::register($this);
/* @var $this yii\web\View */
/* @var $model common\models\business\BusinessContractPurchase */
/* @var $form yii\widgets\ActiveForm */
$total = 0;
?>
<div class="row">
    <div class="col-lg-12">
        <section class="panel">
        <div class="panel-body">
            <?php $form = ActiveForm::begin([
                'options' => ['class' => 'form-horizontal','enctype' => 'multipart/form-data'],
                'fieldConfig' => [
                    'template' => "<div class='col-xs-3 col-sm-2 text-right'>{label}</div><div class='col-xs-9 col-sm-7'>{input}</div><div class='col-xs-12 col-xs-offset-3 col-sm-3 col-sm-offset-0'>{error}</div>",
                ]
            ]); ?>
            <div class="adv-table editable-table ">
                <div class="business-contract-purchase-form">
                        <?= $form->field($model, 'contract_id')->widget(\kartik\select2\Select2::className(), [
                            'options' => ['placeholder' => '请输入合同名称或者编号 ...'],
                            'initValueText'=>!$model->contract?'':$model->contract->name,
                            'pluginOptions' => [
                                'allowClear' => true,
                                'minimumInputLength' => 1,//重要
                                'language' => [
                                    'errorLoading' => new JsExpression("function () { return 'Waiting...'; }"),
                                ],
                                'ajax' => [
                                    'url' => '/business/search/search-contract',
                                    'dataType' => 'json',
                                    'data' => new JsExpression('function(params) { return {q:params.term}; }'),
                                    'cache' => true
                                ],
                                'width' => '400px',
                                'escapeMarkup' => new JsExpression('function (markup) { return markup; }'),
                                'templateResult' => new JsExpression('function(res) { return res.text; }'),
                                'templateSelection' => new JsExpression('function (res) { return res.text; }'),
                            ],
                        ]);
                        ?>

                        <?= $form->field($model, 'number')->textInput(['maxlength' => true,'value'=>$model->isNewRecord?date('YmdHis').rand(10000,99999):$model->number,'readonly'=>true]) ?>
                        <?= $form->field($model, 'name')->textInput(['maxlength' => true]) ?>

                        <?=
                        $form->field($model, 'createtime')->label("购进时间")->widget(DatePicker::classname(), [
                            'options' => ['placeholder' => '购进时间'],
                            'pluginOptions' => [
                                'autoclose'=>true,
                                'format' => 'yyyy-mm-dd'
                            ]
                        ]);
                        ?>

                        <?= $form->field($model, 'connect_person')->textInput(['maxlength' => true]) ?>
                        <?= $form->field($model, 'connect_tel')->textInput(['maxlength' => true]) ?>
                        <?= $form->field($model, 'connect_address')->textInput(['maxlength' => true]) ?>


                        <?= $form->field($model, 'remark')->textarea(['maxlength' => true]) ?>


                </div>
            </div>
            <div class="row">
                <div class="col-md-10 col-md-offset-1">
                    <table class="table table-striped" id="cost">
                        <tr>
                            <th style="width: 10%">仔猪</th>
                            <th style="width: 10%">购进单价 <span style="font-size:10px;">
                                    <input type="text" name="" id="setprice">
                                    <span class="batchsetprice btn btn-primary btn-xs">设置价格</span>
                            </th>
                            <th style="width: 10%">操作 <input type="checkbox" name="" id="allcheck">全选</th>
                        </tr>
                        <?php foreach ($model->pigs as $item => $value)  { ?>
                            <?php $total += $value['cost'] ?>
                            <tr class="costdata">
                                <input type="hidden" name="id[]" value="<?= $value['id'] ?>">
                                <td>
                                    <input type="text" id="pigno" class="form-control" value="<?= $value['no'] ?>"  name="pigno[]" placeholder="仔猪" readonly=true >
                                </td>
                                <td>
                                    <input type="text" id="cost" class="form-control" value="<?= $value['cost'] ?>"  name="cost[]" placeholder="购进价" readonly=true>
                                </td>
                                <td>
                                    <div style="float: left;width: 13%;margin-left: 1%">
                                        <?= Html::button('删除此项',['class'=>'btn btn-warning btn-sm del']) ?>
<!--                                        <input type="checkbox" class="isset" style="float: left">-->
                                    </div>
                                </td>
                            </tr>

                        <?php } ?>

                        <tr class="costdata" id="zongji">
                            <td><b>总计</b></td>
                            <td><b><?= $total ?></b></td>
                            <td></td>
                        </tr>
                    </table>

                    <div class="form-group">
                        <?= Html::button('添加一项明细',['id'=>'ccopy','class'=>'btn btn-info']) ?>
                        <?= Html::submitButton($model->isNewRecord ? '确认提交' : '确认更新', ['class' => $model->isNewRecord ? 'btn btn-success sub' : 'btn btn-primary sub']) ?>

                    </div>

                </div>
            </div>
            <?php ActiveForm::end(); ?>
        </div>
    </section>
</div>
</div>






            <!--选择购进的仔猪模态窗-->
            <div class="modal fade" id="pigmodel" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
                <div class="modal-dialog" role="document">
                    <div class="modal-content">
                        <div class="modal-header">
                            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                            <h4 class="modal-title" id="kucuntitle">购进的仔猪</h4>
                        </div>
                        <div class="modal-body">

                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                            <button type="button" class="btn btn-primary" id="savekucun">保存</button>
                        </div>
                    </div>
                </div>
            </div>
<script>

    <?php $this->beginBlock('js_end') ?>

    $(document).ready(function(){

        //库存的选择的数据，防止前端多次选择库存相同的产品
        var selkudata = [];

        //添加一项
        $('#ccopy').on('click',function (){
            $.post('/business/contract-purchase/search-pigs',{selid:selkudata},function(html){
                $('#pigmodel .modal-body').html(html);
                $('#pigmodel').modal({
                    keyboard: false
                })
            })

        });

        //删除一项
        $('#cost').on('click','.del',function () {
            if (confirm('确定要删除吗？')) {
                $(this).parent().parent().parent('.costdata').remove();
            }
        });


        //删除一项
        $('.batchsetprice').on('click',function () {
            var price = $('#setprice').val();
            console.log(price);
            $('.isset').each(function(){
                if(($(this).is(':checked')) === true){
                    $(this).parent().parent().prev().children().val(price);
                }
//                $(this).removeAttr("checked");
            });
        });


        //保存选择的仔猪
        $('#savekucun').click(function(){
            var data = $('#kucunsel').val();
            if(data === null){
                //没有选关闭窗口
                $('#pigmodel').modal('hide');
                return false;
            }
            selkudata = selkudata.concat(data);
            //选择了，去请求
            $.post('/business/contract-purchase/get-info',{data:data},function(html){
                var str = '';
                $(html).each(function(idnex,res){
                    str += '<tr class="costdata"> <input type="hidden" name="id[]" value="'+res.id+'" readonly=true> <td> <input type="text" id="pig" class="form-control" value="'+res.no+'"  name="pigno[]" readonly=true placeholder="仔猪"> </td> <td> <input type="text" id="cost" class="form-control" value=""  name="cost[]" placeholder="购进价"> </td><td> <div style="float: left;width: 13%;margin-left: 1%"> <?= Html::button('删除此项',['class'=>'btn btn-warning btn-sm del']) ?><input type="checkbox" class="isset" style="float:left">  </div> </td> </tr>';
                });

                $('#pigmodel').modal('hide');
                $('.costdata').last().before(str);
            },'json');
        });



        //全选,取消
        $('#allcheck').on('change',function(){
            if($(this).is(':checked')){
                $('.isset').each(function(){
                    this.checked = true;
                });
            }else{
                $('.isset').each(function(){
                    this.checked = false;
                });
            }
        })
    });



    <?php $this->endBlock(); ?>
    <?php $this->registerJs($this->blocks['js_end'],\yii\web\View::POS_END);//将编写的js代码注册到页面底部 ?>

</script>
